using System;
using System.IO;
using System.Collections.Generic;
using System.ServiceModel.Web;

namespace TicketSellerService {

	public class FlightTicketService : IFlightTicketService {

        print console = Console.WriteLine;

		public AvailableFlights search( string source, string destination, string date) {
            console("Handling request trying to search flight from {0} to {1}", source, destination);
			List<Flight> availFlights = new List<Flight>( );
			foreach( Flight flight in TicketsDB.getInstance( ).getFlights( )) {
				if ( flight.source.Equals( source) && flight.destination.Equals( destination) &&
				     flight.date.Equals( Convert.ToDateTime( date))) {
					availFlights.Add( flight);
				}
			}
            if (availFlights.Count == 0)
            {
                WebOperationContext.Current.OutgoingResponse.SetStatusAsNotFound("Failed, no flights found");
                console("No flight found from {0} to {1} on {2}", source, destination, date);
            }
            else
            {
                WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.OK;
                console("Found {0} flights from {1} to {2} on {3}", availFlights.Count, source, destination, date);
            }
			return new AvailableFlights( availFlights);
		}
		
		public TicketReservation reserve( string flightNumber, string date) {
            console("Trying to reserver flight {0} on {1}", flightNumber, date.ToString( ));
			TicketReservation reservation = null;
			TicketSellerService.TicketsDB.Status status = TicketsDB.getInstance( ).makeReservation( flightNumber, Convert.ToDateTime( date), out reservation);
            if ( reservation == null)
            {
                if (status == TicketsDB.Status.FLIGHT_FULL)
                {
                    WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.BadRequest;
                    WebOperationContext.Current.OutgoingResponse.StatusDescription = String.Format("Failed, no seats available.");
                    console( "No seats available for flight {0} on {1}", flightNumber, date);
                }
                else
                {
                    WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.NotFound;
                    WebOperationContext.Current.OutgoingResponse.StatusDescription = String.Format("Failed, no such flight.");
                    console( "Not flight {0} found on {1} date", flightNumber, date);
                }
            }
            else
            {
                WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.OK;
                reservation.statusMessage = String.Format("OK, reservation ID: {0}", reservation.reservationId);
                console("Reservation for flight {0} on {1} succeded. Reservation ID: {2}", flightNumber, date, reservation.reservationId);
            }
			return reservation;
		}
		
		public string cancel( string rsrvId) {
			TicketsDB.Status status = TicketsDB.getInstance( ).cancelReservation( Convert.ToInt32(rsrvId));
			if ( status == TicketsDB.Status.CANCELATION_FAILED ) {
                WebOperationContext.Current.OutgoingResponse.SetStatusAsNotFound("no reservation found");
                console("Reservation with ID: {0} not found", rsrvId);
				return "Failed, no such reservation";
			}
            WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.OK;
            console("Reservation with ID: {0}  cancelled.", rsrvId);
			return "OK";
		}
		
	}
}